// pages/gjIndex/peccancyBl/pecancyBl.js
import config from "../../../config.js";
const base = config;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //车主信息
    car: null,
    user: null,
    carNo: '', //车牌号
    carId: 0,
    isFirstShow: true,
    wzList: [], //违章列表
    detail: {
      deduct: 0,
      forfeit: 30,
      service: 50
    },
    defaultKeeper: {
      name: '',
      header: '',
      houseKeeperId: null
    },
    userId: null,
    orderId: -1,
    order: null,
    canChange: 1,
    select: {
      selectList: [],
      fen: 0,
      total: 0.0
    }

  },

  search: function() {
    wx.navigateTo({
      url: '../peccancyResult/peccancyResult',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var canChange = options.canChange;
    that.setData({
      canChange: canChange
    })
    if (wx.getStorageSync('user') != null) {
      that.setData({
        'defaultKeeper.name': wx.getStorageSync("user").housekeeper.name,
        'defaultKeeper.header': wx.getStorageSync("user").housekeeper.image,
        'defaultKeeper.houseKeeperId': wx.getStorageSync("user").housekeeper.keeperId
      })
    }


    var that = this;
    //判断是否是再次下单或者更改订单类型
    var orderId = options.orderId;
    var userId = options.uid;

    console.log(orderId);
    console.log(userId);
    if (orderId != null) {
      that.setData({
        orderId: orderId
      })

      //获取订单详情
      that.orderDetail();
    }



  },

  orderDetail() {
    var that = this;
    //获取订单详情
    wx.request({
      url: base.orderDetailUrl,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        id: this.data.orderId,
        isKeeper: 1
      },
      method: "POST",
      success: function(res) {
        console.log(res.data);
        if (res.data.status == 200) {
          that.setData({
            order: res.data.data,
            user: res.data.data.order.account
          })
          that.setOrderData();
        } else {
          wx.showToast({
            title: '订单获取失败！',
            icon: 'none',
            duration: 1500
          })
          return false;
        }
      }

    })
  },
  setOrderData() {
    var that = this;
    //订单信息
    var order = that.data.order;
    console.log(order);
    // var service = that.data.order.service;
    that.setData({
      // typeIndex: order.service.insuranceCom.insuranceId - 1,
      user: order.order.account,
      car: order.order.cars,
      isFirstShow: false
    })
    wx.setStorageSync("userchoose", that.data.user);
    wx.setStorageSync("carchoose", that.data.car);
    // console.log(that.data.typeIndex);
    console.log(that.data.user);
    this.showData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.showData();
  },
  showData: function() {
    var that = this;
    if (that.data.isFirstShow) {
      wx.showToast({
        title: "请选择用户及车辆信息",
        icon: "none"
      })
    } else {
      var item = wx.getStorageSync("userchoose");
      console.log(item);
      if (item != null) {
        this.setData({
          user: item
        })
      }

      var car = wx.getStorageSync("carchoose");
      if (car != null) {
        console.log(car);
        this.setData({
          car: car
        })

        wx.showLoading({
          title: "正在查询"
        })
        wx.request({
          url: base.carIllegalQuery,
          header: {
            'content-type': 'application/x-www-form-urlencoded',
            'Cookie': wx.getStorageSync("session")
          },
          data: {
            id: that.data.car.cid
          },
          success: function(res) {
            console.log(res);
            wx.hideLoading();
            if (res.data.status == 200) {
              that.setData({
                carNo: res.data.hphm,
                wzList: res.data.data
              })
            } else {
              wx.showToast({
                title: res.data.message,
                icon: "none"
              })
            }
          }
        })
      }
    }


    that.setData({
      isFirstShow: false
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    wx.setStorageSync("userchoose", null);
    wx.setStorageSync("carchoose", null);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  exchangeCar: function(e) {
    var userId = e.currentTarget.dataset.userid;
    if (userId) {
      wx.navigateTo({
        url: '../carList/carList?userId=' + userId,
      })
    }
  },
  exchangeCustomer: function(e) {
    wx.navigateTo({
      url: '../customerList/customerList',
    })
  },
  submit: function() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    var user = wx.getStorageSync("userchoose");
    if (this.data.houseKeeperId == "") {
      this.data.houseKeeperId = that.data.defaultKeeper.houseKeeperId;
    }
    var car = wx.getStorageSync("carchoose");
    var carId = car.cid;
    var orderType = 3;
    var orderFrom = 2;
    var time = that.data.date + " " + that.data.time;

    wx.request({
      url: base.carIllegalSubbmit,
      data: {
        userId: user.aid,
        orderId:that.data.orderId,
        houseKeeperId: this.data.houseKeeperId == null ? -1 : this.data.houseKeeperId,
        carId: carId,
        orderType: orderType,
        orderFrom: orderFrom,
        typeId: this.data.detail.deduct == 0 ? 1 : 2,
        selectIds: JSON.stringify(that.data.select.selectList)
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      success: function(res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.status == 200) {
          wx.setStorageSync("orderId", res.data.data.oid);
          wx.setStorageSync("orderConfirmType", 1);
          wx.reLaunch({
            url: "../gjorder/gjorder",
          })
          wx.showLoading({
            title: '订单更改成功！',
          })

        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.message + "",
            icon: 'none',
            mask: true,
            duration: 2000
          })
        }

      },
      error: function() {
        wx.showToast({
          title: "请求失败,请联系管理员",
          icon: 'none',
          mask: true,
          duration: 2000
        })
      }
    })
  },
  selectCustomer: function(e) {
    wx.navigateTo({
      url: '../customerList/customerList',
    })
  },
  exchangeCar: function(e) {
    var id = e.currentTarget.dataset.userid;
    wx.navigateTo({
      url: '../carList/carList?userId=' + id,
    })
  },
  selectItemAction: function(e) {
    var that = this;
    console.log(e);
    var selectList = e.detail.value;
    console.log(selectList);
    var total = 0;
    var fen = 0;

    for (var j = 0; j < selectList.length; j++) {
      for (var i = 0; i < that.data.wzList.length; i++) {
        var item = that.data.wzList[i];
        if (item.peccancyId == selectList[j]) {
          fen += item.fen;
          total += item.money;
        }
      }
    }

    //设置数据
    that.setData({
      'select.selectList': selectList,
      'select.fen': fen,
      'select.total': total
    })

    console.log(that.data);
  },
  selectItemAction: function(e) {
    var that = this;
    console.log(e);
    var selectList = e.detail.value;
    console.log(selectList);
    var total = 0;
    var fen = 0;

    for (var j = 0; j < selectList.length; j++) {
      for (var i = 0; i < that.data.wzList.length; i++) {
        var item = that.data.wzList[i];
        if (item.peccancyId == selectList[j]) {
          fen += item.fen;
          total += item.money;
        }
      }
    }

    //设置数据
    that.setData({
      'select.selectList': selectList,
      'select.fen': fen,
      'select.total': total
    })

    console.log(that.data);
  },
})